package view
{
	import com.bit101.components.ColorChooser;
	import com.bit101.components.ComboBox;
	import com.bit101.components.MySlider;
	import com.bit101.components.Slider;
	
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import models.ModelLocator;
	
	public class EditTxt extends Sprite
	{
		private var _console:Console;
		private var _text:TextSprite;
		private var txtinput:TextField;
		private var model:ModelLocator = ModelLocator.getInstance();
		private var comobox:ComboBox;
		public function EditTxt(source:String)
		{
			init();
			this.x = 200;
			this.y = 300;
			var urlloader:URLLoader = new URLLoader;
			var urlreq:URLRequest = new URLRequest(source);
			urlloader.addEventListener(Event.COMPLETE,onLoadcomplete);
			urlloader.load(urlreq);
		}
		private function init():void{
			draw();
			initInput();
			initTitle();
			intitFontList();
			initSlider();
			initDel();
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
		}
		private function onMouseDown(event:MouseEvent):void{
			if(this.mouseY < 10){
				this.startDrag();
				stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			}
		}
		private function onMouseUp(event:MouseEvent):void{
			this.stopDrag();
		}
		private function intitFontList():void{
			comobox = new ComboBox(this,44,77);
			comobox.width = 72;
			//comobox.items = ary;
			comobox.addEventListener(Event.SELECT,onSelectFont);
			comobox.defaultLabel = "正在加载";
			/*if(model.fontList){
				comobox.defaultLabel = "正在加载";
			}else{
				comobox.defaultLabel = "微软雅黑";
			}*/
			
		}
		private function onLoadcomplete(event:Event):void{
			var str:String = event.target.data as String;
			var ary:Array = str.split(',');
			setFontList(ary)
		}
		public function setFontList(otherList:Array):void{
			var allFonts:Array = Font.enumerateFonts(true);
			allFonts.sortOn("fontName", Array.DESCENDING);
			
			var ary:Array = new Array;
			for(var i:uint=0;i<allFonts.length;i++){
				for(var j:uint=0;j<otherList.length;j++){
					if(allFonts[i].fontName == otherList[j]){
						var obj:Object = new Object;
						obj.label = allFonts[i].fontName;
						ary.push(obj);
					}
				}
			}
			
			comobox.items = ary;
			for(i=0;i<ary.length;i++){
				if(ary[i].label == "微软雅黑"){
					comobox.defaultLabel = "微软雅黑";
					return;
				}
			}
			comobox.defaultLabel = "宋体";
		}
		private function initInput():void{
			txtinput = new TextField;
			txtinput.width = 100;
			txtinput.height = 45;
			txtinput.x = 12;
			txtinput.y = 10;
			txtinput.type = TextFieldType.INPUT;
			txtinput.multiline = true;
			txtinput.textColor = 0xdedede;
			this.addChild(txtinput);
			txtinput.addEventListener(Event.CHANGE,onChange);
		}
		private function initTitle():void{
			var textfomat:TextFormat = new TextFormat();
			textfomat.leading = 15;
			var txt:TextField = new TextField;
			txt.width = 50;
			txt.height = 150;
			txt.x = 12;
			txt.y = 75;
			txt.multiline = true;
			txt.textColor = 0xdedede;
			txt.selectable = false;
			txt.defaultTextFormat = textfomat;
			txt.text = "宋体\n字号\n透明";
			this.addChild(txt);
		}
		private function initSlider():void{
			var fontsize:MySlider = new MySlider(Slider.HORIZONTAL,this,44,110);
			fontsize.maximum = 140;
			fontsize.minimum = 0;
			fontsize.value = 15;
			fontsize.width = 72;
			fontsize.addEventListener(Event.CHANGE,onSizeChange);
			
			var alphaSize:MySlider = new MySlider(Slider.HORIZONTAL,this,44,140);
			alphaSize.maximum = 1;
			alphaSize.minimum = 0;
			alphaSize.value = 1;
			alphaSize.width = 72;
			alphaSize.addEventListener(Event.CHANGE,onAlphaChange);
			
			var color:ColorChooser = new ColorChooser(this,-36,160,0x000000);
			color.usePopup = true;
			color._input.visible = false;
			color.addEventListener(Event.CHANGE,onColor);
		}
		private function initDel():void{
			var txt:TextField = new TextField;
			txt.width = 100;
			txt.height = 45;
			txt.x = 60;
			txt.y = 160;
			txt.textColor = 0xdedede;
			txt.htmlText = "<a href='event:texte'><u>删除</u></a>";
			txt.addEventListener(MouseEvent.CLICK,onDelClick);
			this.addChild(txt);
		}
		private function onDelClick(event:MouseEvent):void{
			if(_console && _console.parent){
				_console.parent.removeChild(_console);
				this.visible = false;
			}
		}
		private function draw():void{
			
			var fillType:String = GradientType.LINEAR;
			var colors:Array = [0x6e6e6e, 0x3d3d3d];
			var alphas:Array = [1, 1];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(20, 194, Math.PI/2, 0, 0);
			var spreadMethod:String = SpreadMethod.PAD;
			this.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);  
			this.graphics.drawRoundRect(0,0,124,194,8,8);
			this.graphics.endFill();
			
			this.graphics.beginFill(0x333333);
			this.graphics.drawRect(12,10,100,45);
			this.graphics.endFill();
		}
		private function onChange(event:Event):void{
			_text.text = txtinput.text;
		}
		public function set console(value:Console):void
		{
			_console = value;
			_console.edit = this;
			_text = _console.targets as TextSprite;
		}
		public function onSelectFont(event:Event):void{
			if(event.target.selectedItem == null){
				return;
			}
			_text.fontface = event.target.selectedItem.label;
		}
		public function onColor(event:Event):void{
			var str:String = event.target._input.text;
			_text.color = str;
		}
		public function onSizeChange(event:Event):void{
			_text.size = event.target.value;
		}
		public function onAlphaChange(event:Event):void{
			_text.alpha = event.target.value;
		}
	}
}